package com.truyentranh.main;

import java.io.File;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.callback.ImageOptions;
import com.androidquery.util.AQUtility;
import com.truyentranh.R;
import com.truyentranh.network.RequestBackgroundWorker;
import com.truyentranh.utils.GlobalData;

public class MainApplication extends Application {
	private static Context context;

	public static Context getAppContext() {
		return MainApplication.context;
	}

	@Override
	public void onCreate() {
		try {
			MainApplication.context = getApplicationContext();

			String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
			AQUtility.setCacheDir(new File(extStorageDirectory, "Haibua/cache"));

			// set the max number of concurrent network connections, default is
			// 4
			AjaxCallback.setNetworkLimit(4);
			BitmapAjaxCallback.setTimeout(30000);

			// set the max number of images (image width > 50) to be cached in
			// memory, default is 20
			BitmapAjaxCallback.setCacheLimit(20);

			// set the max size of an image to be cached in memory, default is
			// 3600
			// pixels (ie. 600x600)
			BitmapAjaxCallback.setPixelLimit(1280 * 1280);

			// set the max size of the memory cache, default is 1M pixels (4MB)
			// BitmapAjaxCallback.setMaxPixelLimit(3000000);
			// fix me
			ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
			int available = activityManager.getMemoryClass() * 1000000;
			BitmapAjaxCallback.setMaxPixelLimit((int) (available / 4) * 40 / 100);

			new Thread(new Runnable() {
				public void run() {
					initOptAQuery();
					initGlobalData();
				}
			}).start();
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.onCreate();
	}

	@Override
	public void onLowMemory() {
		cleanAllCache();
	}

	public void initOptAQuery() {
		try {
			if (GlobalData.mAQ == null)
				GlobalData.mAQ = new AQuery(getApplicationContext());

			if (GlobalData.opt_avatar == null) {
				GlobalData.opt_avatar = new ImageOptions();
				GlobalData.opt_avatar.round = 10;
				GlobalData.opt_avatar.memCache = true;
				GlobalData.opt_avatar.fileCache = true;
				GlobalData.opt_avatar.preset = BitmapFactory.decodeResource(getResources(), R.drawable.no_preview);
				GlobalData.opt_avatar.fallback = R.drawable.no_preview;
				GlobalData.opt_avatar.animation = AQuery.FADE_IN_FILE;
			}

			if (GlobalData.opt_gallery_full == null) {
				GlobalData.opt_gallery_full = new ImageOptions();
				GlobalData.opt_gallery_full.memCache = true;
				GlobalData.opt_gallery_full.fileCache = true;
				GlobalData.opt_gallery_full.targetWidth = 400;
				GlobalData.opt_gallery_full.preset = null;
				// GlobalData.opt_gallery_full.fallback =
				// R.drawable.ic_gallery_holder;
				GlobalData.opt_gallery_full.animation = AQuery.FADE_IN_NETWORK;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initGlobalData() {
		try {
			startWorker();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void startWorker() {
		 RequestBackgroundWorker.startWaitingForRequest();
	}

	public static void cleanAllCache() {
		try {
			 BitmapAjaxCallback.clearCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
